package com.mmlc.meimei.lichengkongjian.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.SimpleTagImageView;
import com.mmlc.network.responsebean.MeiQiFastListResponseBean;
import com.mmlc.utils.XUtils;

import java.util.List;

/**
 * Created by Administrator on 2016/12/5.
 */
public class FastBuyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public Context context;
    List<MeiQiFastListResponseBean.DataBean.GoodslistBean> data;

    private OnItemClickListener clickListener;

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public static interface OnItemClickListener {
        void onClick(View view, int position);
    }

    public FastBuyAdapter(Context context, List<MeiQiFastListResponseBean.DataBean.GoodslistBean> data) {
        this.context = context;
        this.data = data;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fastbuy, null);
        AuthorViewHolder  vh = new AuthorViewHolder (view);
        return vh;
    }

    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {

        // 如果设置了回调，则设置点击事件

        final AuthorViewHolder vh = (AuthorViewHolder) holder;
        vh.tvTitlefirst.setText(data.get(position).getSub_name());
        vh.tvTitlesecond.setText(data.get(position).getGoods_name());

        String price = data.get(position).getPrice();
        if(!TextUtils.isEmpty(price)){
            vh.tvPrice.setText("¥ "+price);
        }
        String market_price = data.get(position).getMarket_price();
        if(!TextUtils.isEmpty(market_price)&& !market_price.equals(price)) {
            vh.tvMarketPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            vh.tvMarketPrice.setText("¥ " + market_price);
        }else{
            vh.tvMarketPrice.setVisibility(View.GONE);
        }

        vh.tvLovecount.setText(data.get(position).getCollect_num());
        vh.tv_times.setTagText("第"+data.get(position).getBatch()+"期");
        if(data.get(position).getIs_collected() .equals("1") ){
            vh.ivLike.setImageDrawable(context.getResources().getDrawable(R.drawable.liked));
        }else{
            vh.ivLike.setImageDrawable(context.getResources().getDrawable(R.drawable.like));
        }
        if (!TextUtils.isEmpty(data.get(position).getImg())) {
            XUtils.image(vh.ivGoodspic, data.get(position).getImg(), XUtils.getImageOptionsBuilder().setImageScaleType(ImageView.ScaleType.CENTER_CROP).build());
        }
        if(data.get(position).getButton_color()==1){
            vh.tvDo.setText(data.get(position).getSchedule());
            vh.tvDo.setTextColor(Color.WHITE);
            vh.tvDo.setBackgroundColor(Color.parseColor("#0b0b0b"));
        }else {
            vh.tvDo.setText(data.get(position).getSchedule());
            vh.tvDo.setTextColor(Color.parseColor("#282a2b"));
            vh.tvDo.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_border));

        }

        if(clickListener != null){
            //为ItemView设置监听器
            vh.ivGoodspic.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getLayoutPosition(); // 1
                    clickListener.onClick(vh.ivGoodspic,position); // 2
                }
            });

            vh.tvDo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getLayoutPosition(); // 1
                    clickListener.onClick(vh.tvDo,position); // 2
                }
            });

            vh.ivLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getLayoutPosition(); // 1
                    clickListener.onClick(vh.ivLike,position); // 2
                }
            });
            vh.llCollect.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = vh.getLayoutPosition(); // 1
                    clickListener.onClick(vh.llCollect,position); // 2
                }
            });
        }

        if(position == data.size()-1){
            vh.view_line.setVisibility(View.GONE);
        }else{
            vh.view_line.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class AuthorViewHolder   extends RecyclerView.ViewHolder {

        ImageView ivGoodspic,ivLike;
        TextView tvLovecount,tvTitlefirst,tvTitlesecond,tvPrice,tvMarketPrice,tvDo;
        LinearLayout llCollect;
        RelativeLayout rlHead;
        SimpleTagImageView tv_times;
        View view_line;
        public AuthorViewHolder (View itemView) {
            super(itemView);
            ivGoodspic = (ImageView) itemView.findViewById(R.id.iv_goodspic);
            ivLike = (ImageView) itemView.findViewById(R.id.iv_like);
            tv_times = (SimpleTagImageView) itemView.findViewById(R.id.tv_times);
            tvLovecount = (TextView) itemView.findViewById(R.id.tv_lovecount);
            tvTitlefirst = (TextView) itemView.findViewById(R.id.tv_titlefirst);
            tvTitlesecond = (TextView) itemView.findViewById(R.id.tv_titlesecond);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvMarketPrice = (TextView) itemView.findViewById(R.id.tv_market_price);
            tvDo = (TextView) itemView.findViewById(R.id.tv_do);
            rlHead = (RelativeLayout) itemView.findViewById(R.id.rl_head);
            llCollect = (LinearLayout) itemView.findViewById(R.id.ll_collect);
            view_line = itemView.findViewById(R.id.view_line);
        }
    }
}